/* 
 @description: 协议管理

 @author: 韦淇霰

 @update: 2021年9月12日00:55:35
 */
 const add= document.getElementById('add'),
 input =document.querySelector('input'),
 back = document.querySelector('.back'),
 submit = document.getElementById('submit'),
 closes = document.getElementById('closes'),
 search = document.querySelector('.Search'),
 xy_title = document.getElementById('xy_title'),
 xy_content = document.getElementById('xy_content'),
 xy_time = document.getElementById('xy_time'),
 table = document.getElementById('tab');
 let isEdit = 0;    
 getList();
     function getList(keyname='') {
        $.ajax({
        url: `http://127.0.0.1:7001/api/agreement?keyname=${keyname}`,
        method: "GET",
        success: function(res) {
                for (var i = 0; i < res.data.length; i++) {
                    const tr = document.createElement('tr');
                    for (let data in res.data[i]) {
                    const td = document.createElement("td");
                        td.innerHTML = res.data[i][data]
                        // console.log(res.data[i].action_on);
                        tr.appendChild(td); 
                        if(data === 'agr_time'){
                            let agr_time = res.data[i].agr_time;
                            var times = new Date(parseInt(agr_time));
                            var Y = times.getFullYear()+'-';
                            var M = (times.getMonth()+1<10 ? '0'+( times.getMonth()+1): times.getMonth()+1) + '-';
                            var D = times.getDate()< 10 ? '0' + times.getDate():times.getDate()+ " ";
                            var H = times.getHours()<10 ? '0' + times.getHours() : times.getHours()  + ":";
                            var mi = times.getMinutes() < 10 ? '0' + times.getMinutes() : times.getMinutes() + ":";
                            var S = times.getSeconds() < 10 ? '0' + times.getSeconds() : times.getSeconds() + " ";
                            // console.log(Y+M+D,H+mi+S);
                            td.innerHTML=Y+M+D+" "+H+mi+S;
                        }
                        if(data === 'action_on'){
                            const img = document.createElement('img');
                            td.appendChild(img);
                            img.src=`http://192.168.3.161:7001${res.data[i].action_on}`
                            // console.log(res.data[i].action_on);
                        }

                    } 

                    const lasttd = document.createElement('td');
                    lasttd.innerHTML  = `<button type='button'  name="updata" class="up"  id='${res.data[i].id}'><i class="fa fa-wrench" aria-hidden="true"></i> 修改</button>`
                    tr.appendChild(lasttd)
                    tr.id = res.data[i].id;
                    table.appendChild(tr);
                }
            }  
        }
    )}
 //编辑
 function EditCa(agr_title, agr_content, agr_time ,id){
     $.ajax({
     url:`http://127.0.0.1:7001/api/agreement/${id}`,
     method:"put",
     data:{
        agr_title ,
        agr_content,
        agr_time ,
        id
     },
     success:function(res){
         console.log(res);
         if(res.code === 0){
         window.location.reload();
         closeBack();  
         }else{
             alert(res.msg);
         }
     }
 })
 }
 //获取详情
 function getInfo(id){
     $.ajax({
     url:`http://127.0.0.1:7001/api/agreement/${id}`,
     method:"get",
     data:{
         id,
     },
     success:function(res){
        const fwb = document.querySelector('.w-e-text');
         console.log(res);
         if(res.code === 0){
         xy_title.value = res.data.agr_title;
         fwb.innerHTML= res.data.agr_content;
         xy_time.value = res.data.agr_time;
         }else{
             alert(res.msg)
         }
         console.log(res);
     }
     
     })
 }
 let ida=0;
 //通过事件委托操作编辑
 table.onclick = function(e){
    if(e.target.className === 'up'){
         back.style.display = 'block';
         const id  = e.target.id;
            ida = id
         getInfo(id);
     }
 }
    
 //编辑提交按钮
 submit.onclick = function(){
    // const a = editor.txt.text();
    const a = editor.txt.html();
    console.log(a);
    EditCa(xy_title.value,a,xy_time.value,ida);
 }
 closes.onclick = function(){
    xy_title.value = '';
    xy_content.value ='';
    xy_time.value ='';
    back.style.display = 'none';
 }
   //富文本
   const E = window.wangEditor;
   const editor = new E('#xy_content');
   editor.create();


 